<?php
    namespace Home\Controller;
    use Think\Controller;
    class KeshichengbenmingxiController extends Controller {
    
    public function _initialize()
    {
        header("Access-Control-Allow-Origin: *");  

        header("Access-Control-Allow-Credentials: true"); 
      
        header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS"); //允许的请求类型
        
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept"); 

    }

    public function get_list()
    {
        
		
		$page = I("get.page");
		$limit = I("get.limit");
		$begin_time = I("get.begin_time");
		$end_time = I("get.end_time");
		$dept_code = I("get.dept_code");
		
		$where = " a.DEPT_CODE = B.DEPT_CODE
		 AND a.COST_ITEM_CODE = c.COST_ITEM_CODE ";
		
		if(!empty($dept_code))
		{
		    $where .= " and a.DEPT_CODE = '$dept_code'";
		}
		
		if(empty($begin_time))
		{
		    $begin_time = 0;
		}
		else
		{
		    $begin_time = strtotime($begin_time);
		    $where .= " and a.ACCOUNTING_DATE >= '$begin_time'";
		}
		
		if(empty($end_time))
		{
		    $end_time = 9999999999;
		}
		else
		{
		    $end_time = strtotime($end_time);
		    $where .= " and a.ACCOUNTING_DATE <= '$end_time'";
		}
		
		
		
		$M = M("");
		$data = $M ->table("CBHS.CBHS_DEPT_COST_DETAIL a,comm.sys_dept_dict b,CBHS.CBHS_COST_ITEM_DICT c")->where($where)->field("c.COST_TYPE_NAME as COST_TYPE_NAME,
		 c.COST_ITEM_NAME as COST_ITEM_NAME,
		 SUM (a.COSTS) as COSTS,
		 SUM (a.COSTS_ARMYFREE) as COSTS_ARMYFREE,
		 SUM (a.REAL_COSTS) as REAL_COSTS")->group("c.COST_TYPE_NAME, c.COST_ITEM_NAME")->select();
		// echo $M->getLastSql();
		// die();
		for ($i=0; $i < count($data); $i++) 
		{ 
		    $info = $data[$i];
		}
		$json["data"] = $data;
		$json["code"] = 0;
		// $json["count"] = M("")->table("CBHS.CBHS_DEPT_COST_DETAIL a,comm.sys_dept_dict b,CBHS.CBHS_COST_ITEM_DICT c")->where($where)->field("c.COST_TYPE_NAME as COST_TYPE_NAME,
		//  c.COST_ITEM_NAME as COST_ITEM_NAME,
		//  SUM (a.COSTS) as COSTS,
		//  SUM (a.COSTS_ARMYFREE) as COSTS_ARMYFREE,
		//  SUM (a.REAL_COSTS) as REAL_COSTS")->group("c.COST_TYPE_NAME, c.COST_ITEM_NAME")->count();
		$json["count"] = 0;
		$json["msg"] = "";
		
		$this->ajaxReturn($json,"json");
    }

    public function get_one()
    {
        $uuid = I("post.uuid");

        $info = M("cbhs.cbhs_dept_cost_detail")->where("uuid = '$uuid'")->find();
        $info["dept_date"] = date("Y-m-d H:i:s",$info["dept_date"]);
        $json["data"]   = $info;
        $json["code"]   = 0;
        $json["msg"]    = "";

        $this->ajaxReturn($json,"json");
    }

    public function save_one()
    {
        $data = I("post.");
        $uuid = $data["uuid"];
        unset($data["uuid"]);

        $result = M("cbhs.cbhs_dept_cost_detail")->where("uuid = '$uuid'")->save($data);
        if($result !== false)
        {
            $json["code"] = 0;
            $json["msg"] = "修改成功";
        }
        else
        {
            $json["code"] = -1;
            $json["msg"] = "修改失败";
        }
 
        $this->ajaxReturn($json,"json");
    }

    public function add_one()
    {
        $data = I("post.");
        $uuid = uuid();
        
        $data["dept_date"] = time();
        $result = M("cbhs.cbhs_dept_cost_detail")->add($data);
        if($result !== false)
        {
            $json["code"] = 0;
            $json["msg"] = "添加成功";
        }
        else
        {
            $json["code"] = -1;
            $json["msg"] = "添加失败";
        }
 
        $this->ajaxReturn($json,"json");
    }

    public function get_sys_dept_dict()
    {
		$admin_uuid = I('admin_uuid');
		$admin_dept_list_str = get_user_dept_list($admin_uuid);
		
        $list = M("comm.sys_dept_dict")->where("dept_code in ($admin_dept_list_str)")->select();
        $json["data"]   = $list;
        $json["code"]   = 0;
        $json["msg"]    = "";
        $this->ajaxReturn($json,"json");
    }
   }